package org.duang.kit;

import org.duang.event.EventUtil;
import org.duang.event.core.EventModel;

/**
 * 事件监听器工具类
 * @author laotang
 */
public class EventKit {
	
	private static java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock();
	
//	private static Event event;
	private static EventModel model;
	private static EventKit eventKit;

	public static EventKit duang(){
		synchronized (lock) {
			if (null == eventKit) {
				eventKit = new EventKit();				
			}
		}
//		event = null;
		model = new EventModel();
		return eventKit;
	}
	
	public EventKit key(String key) {
		model.setKey(key);
		return this;
	}
	
	public EventKit param(Object value) {
		model.setModel(value);
		return this;
	}
	
	public EventKit aync(boolean aync) {
		model.setAync(aync);
		return this;
	}
	
//	@Deprecated
//	public EventKit event(Event event) {
//		EventKit.event = event;
//		return this;
//	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T execute() {
		if(ToolsKit.isNotEmpty(model.getModel())) {
//			EventKit.event = null;
			return (T)EventUtil.executeEvent(EventKit.model);
		} 
		return null;
//		else if(EventKit.event  != null) {
//			EventUtil.executeEvent(EventKit.event);
//		}
	}
}
